<?php
namespace Rainrock\Framework\Controller;

use Rainrock\Framework\kernel\Html\HtmlBase;
use Rainrock\Framework\kernel\Html\HtmlLabel;
use Rainrock\Framework\kernel\Html\HtmlTable;
use Rainrock\Framework\kernel\Html\HtmlInput;
use Rainrock\Framework\kernel\Html\HtmlButton;
use Rainrock\Framework\kernel\Html\HtmlImage;
use Rainrock\Framework\kernel\Html\HtmlSelect;
use Rainrock\Framework\kernel\Html\HtmlTextarea;
use Rainrock\Framework\kernel\base\Web;
use Rainrock\Framework\kernel\base\Auth;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Jiami;
use Rainrock\Framework\kernel\base\File;
use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\CDate;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\core\Module;

/**
*	管理首页
*/
class AdminmanageController extends AuthController{
	
	
	public function checkAccess()
	{
		$str = parent::checkAccess();
		if($str)return $str;
		if(Auth::$TYPE !=2 && ACTION != 'index')return 'not Auth';
	}

	private function getMenu()
	{
		$mode = Module::get('sysmenu', 'system');
		if(!$mode)return false;
		$rows = $this->getMenus($mode->getModel(), 0);
		return $rows;
	}
	private function getMenus($db, $pid){
		$where 	= '';
		if(Auth::$TYPE==0)$where='AND `type` = 0';
		if(Auth::$TYPE==1)$where='AND `type` in(0,1)';
		$rows 	= $db->getalls('`status`=1 and `pid`='.$pid.' '.$where.'','*','`sort`');
		if($rows)foreach($rows as $k=>$rs){
			$rows[$k]['menusub'] = $this->getMenus($db, $rs['id']);
		}
		return $rows;
	}
	
	public function indexAction()
	{
		$html = new HtmlBase();
		$html->setTitle(TITLE);
		$html->addJse('adminmanage');
		$html->addJse('table');
		$html->addCss('table');
		$html->addJse($this->c('rockfile')->importurl(), true);
		
		$menudiv = HtmlLabel::get()->setHeight(500,'auto')->setId('menudiv')->addAttrs('resizeh','60');
		$rows 	 =$this->getMenu();
		if($rows)foreach($rows as $k=>$rs){
			$menusub = $rs['menusub'];
			$jiang 	 = ($menusub)? '■':'□';
			$mdiv 	 = HtmlLabel::get()->addCont($jiang)->addKong()->addCont($rs['name'])->addCss('hover cursor')->setAlign('left')->addStyle('padding','15px 10px');
	
			if($menusub){
				$mdiv->addCont(HtmlLabel::get('span')->addStyle('float','right')->addCont('&gt;'));
				$mdiv->setonClick('ind.menuhide('.$rs['id'].')');
			}else{
				$mdiv->setonClick('ind.menuclick('.$rs['id'].',\''.$rs['name'].'\',\''.$rs['url'].'\',\''.$rs['num'].'\')');
			}
			
			$menudiv->addCont($mdiv);
			if($menusub){
				$sdiv = HtmlLabel::get()->setId('menulist_'.$rs['id'].'')->setDisplay(($rs['isopen']==1) ? '' : 'none');
				foreach($menusub as $k1=>$rs1){
					$mdiv1 = HtmlLabel::get()->addCont('◇')->addKong()->addCont($rs1['name'])->addCss('hover cursor')->setAlign('left')->addStyle('padding','15px 10px 15px 30px');
					$mdiv1->setonClick('ind.menuclick('.$rs1['id'].',\''.$rs1['name'].'\',\''.$rs1['url'].'\',\''.$rs1['num'].'\')');
					$sdiv->addCont($mdiv1);
				}
				$menudiv->addCont($sdiv);
			}
		}
		
		$leftdiv = HtmlLabel::get()->setWidth(220)
		->setFontcolor('white')
		->setBackgroundColor('var(--main-color)')
		->addCont(
			HtmlLabel::get()->setHeight(60,'hidden')->setBackgroundColor('rgba(0,0,0,0.1)')->setDisplay('flex')
			->addStyle('align-items','center')->setonClick('js.reload()')->addCss('cursor')
			->addCont(
				HtmlLabel::get()->addStyle('padding','0px 5px')->setHeight(30,'hidden')
				->addCont(HtmlImage::gets()->setSrc(''.URL.'images/logo.png')->setWidth(30)->setHeight(30)->setTitle(TITLE)->setRadius(5))
			)
			->addCont(
				HtmlLabel::get()->addCont(TITLE)->setFontsize(18)->addStyle('padding-right','5px')
			)
		)
		->addCont($menudiv)
		->setId('leftdiv');
		if(Web::isMobile())$leftdiv->setDisplay('none');
		
		
		$tabdiv = HtmlLabel::get()->setHeight(58,'hidden')
		->setBackgroundColor('rgba(255,255,255,0.1)')
		->addStyle('border-bottom','2px rgba(0,0,0,0.05) solid')
		->addCont(
			HtmlLabel::get('ul')->addCss('tabsindex')
			->addCont(
				HtmlLabel::get('li')->addCont('☰')->setonClick('ind.hideleft(this)')
			)
		)
		->addCont(
			HtmlLabel::get()->addStyle('flex',1)
			->addCont(
				HtmlLabel::get('ul')->addCss('tabsindex')->setId('tabs_title')->setDisplay('flex')
			)
		)
		->addCont(
			HtmlLabel::get('ul')->addCss('tabsindex')
			->addCont(
				HtmlLabel::get('li')->setonClick('ind.geren()')->addCont(
					HtmlLabel::get()
					->addCont(HtmlImage::gets()->setRadius('50%')->setWidth(20)->setHeight(20)->setAlign('absmiddle')->setSrc(Auth::getUserInfo('face'))->setId('myfaces'))
					->addKong()
					->addCont(Auth::getUserInfo('name'))
				)
			)
		)
		->setDisplay('flex');
		
		
		$rightdiv = HtmlLabel::get()
		->addCont($tabdiv)->addStyle('overflow','hidden')
		->addCont(
			HtmlLabel::get()->setHeight(500,'auto')->setId('indexcontent')->addAttrs('resizeh','60')->addStyle('position','relative')
			->addCont(
				HtmlLabel::get()->setId('content_allmainview')->addStyle('margin','10px')
			)
		)
		->addStyle('flex',1);
		
		$html->addCont(
			HtmlLabel::get()
			->setDisplay('flex')
			->addCont($leftdiv)
			->addCont($rightdiv)
		);
		$html->addStyle('.tabsindex li{line-height:58px;height:58px;padding:0px 15px;cursor:pointer;position:relative;color:#888888}');
		$html->addStyle('.tabsindex li:hover{background:rgba(0,0,0,0.05);border-radius:5px 5px 0px 0px;}');
		$html->addStyle('.tabsindex .active{font-weight:bold;background:rgba(0,0,0,0.05);border-radius:5px 5px 0px 0px;color:'.($html->isBlack() ? 'inherit' : 'var(--main-color)').'}');
		$html->addStyle('.tabsindex span{font-size:12px;color:#aaaaaa;position:absolute;top:2px;right:2px;display:inline-block;height:16px;width:16px;line-height:14px;text-align:center;}');
		$html->addStyle('.tabsindex span:hover{background-color:rgba(0,0,0,0.1);color:white;border-radius:50%}');

		return $html->show();
	}
	
	
	/**
	*	更新管理的
	*/
	public function modeupdateAction()
	{
		$mdiv2 = HtmlLabel::get()->setAlign('left')
				->setWidth('300px')
				->addCss('panel')
				->addStyle('margin-top','20px');
		
		$mdiv2->addCont(
			HtmlLabel::get()->addCont('模块列表(点击可更新)')->addCss('panel-title')
		);
		
		
		$sdiv = HtmlLabel::get()->addCss('list-group');
		
		$sdiv->addCont(
			HtmlLabel::get()->addCss('list-group-item')
			->setonClick('js.open(\'?m=database&a=daochu\')')
			->addCont('导出数据库')
		);
		
		
		$sdiv->addCont(
			HtmlLabel::get()->addCss('list-group-item')
			->setonClick('js.open(\'?m=database&a=update&num=all\')')
			->addCont('全部更新')
		);
		
		$sdiv->addCont(
			HtmlLabel::get()->addCss('list-group-item')
			->setonClick('js.open(\'?m=webstatic&a=update\')')
			->addCont('更新前端文件(必须点)')
		);
		
		$groups		= Module::getList();
		foreach($groups as $gs=>$als){
			$sdiv->addCont(
				HtmlLabel::get()->addCss('list-group-item')
				->setonClick('js.open(\'?m=database&a=update&group='.$gs.'\')')
				->addCont('组.')
				->addStyle('font-weight','bold')
				->addCont($gs)
			);
			foreach($als as $nu1){
				$info = Module::get($nu1, $gs);
				if($info){
					$sdiv->addCont(
						HtmlLabel::get()->addCss('list-group-item')->addStyle('padding-left','30px')
						->setonClick('js.open(\'?m=database&a=update&num='.$nu1.'&zhu='.$gs.'\')')
						->addCont($info->getName().'('.$nu1.')')
					);
				}
			}
		}
		
		$mdiv2->addCont($sdiv);
		return $mdiv2->show();
	}
	
	
	/**
	*	系统设置
	*/
	public function cogAction()
	{
		$mdiv = HtmlLabel::get();
		$mdiv->addBlank(20);
		
		$mdiv2 = HtmlLabel::get()->setAlign('left')->addCss('list-group');
				
		$mdiv2->addCont(
			HtmlLabel::get()->addCss('flex')
			->addCont(HtmlLabel::get()->addCont('系统标题')->addBr()->addCont(HtmlLabel::get()->setFontsize(12)->addCss('hui')->addCont('title'))->addStyle('margin-right','10px')->setWidth(120)->setAlign('right'))
			->addCont(HtmlInput::gets()->setValue(TITLE)->addStyle('flex', 1))
		);
		$mdiv2->addBlank(20);
		$mdiv2->addCont(
			HtmlLabel::get()->addCss('flex')
			->addCont(HtmlLabel::get()->addCont('主题色')->addBr()->addCont(HtmlLabel::get()->setFontsize(12)->addCss('hui')->addCont('apptheme'))->addStyle('margin-right','10px')->setWidth(120)->setAlign('right'))
			->addCont(
				HtmlLabel::get()->addStyle('flex', 1)->addCss('flex webbtn-group')
				->addCont(HtmlInput::gets()->setValue(APPTHEME))
				->addCont(HtmlButton::gets()->addCont('选')->setClickevt('changecolor')->addCss('webbtn-default'))
			)
		);
		
		$mdiv2->addBlank(20);
		$larr[] = array('name'=>'仅帐号密码(对应值0)','value'=>'0');
		$larr[] = array('name'=>'手机验证码验证(对应值1)','value'=>'1');
		$larr[] = array('name'=>'微信验证码验证(对应值2)','value'=>'2');
		$mdiv2->addCont(
			HtmlLabel::get()->addCss('flex')
			->addCont(HtmlLabel::get()->addCont('登录类型')->addBr()->addCont(HtmlLabel::get()->setFontsize(12)->addCss('hui')->addCont('loginmode'))->addStyle('margin-right','10px')->setWidth(120)->setAlign('right'))
			->addCont(HtmlSelect::gets()->setValue(Base::getConfig('loginmode'))->setData($larr)->addStyle('flex', 1))
		);
		
		$mdiv2->addBlank(10);
		$mdiv2->addCont(HtmlLabel::get()->setAlign('center')->addCont('文件上传中心，用于上传文件使用')->setPadding(5)->setFontsize(12)->addCss('hui'));
		$mdiv2->addCont(
			HtmlLabel::get()->addCss('flex')
			->addCont(HtmlLabel::get()->addCont('文件上传中心')->addBr()->addCont(HtmlLabel::get()->setFontsize(12)->addCss('hui')->addCont('rockfile_url'))->addStyle('margin-right','10px')->setWidth(120)->setAlign('right'))
			->addCont(HtmlInput::gets()->setTishi('用不到不用设置')->setValue(Base::getConfig('rockfile_url'))->addStyle('flex', 1))
		);
		
		$mdiv2->addBlank(20);
		$mdiv2->addCont(
			HtmlLabel::get()->addCss('flex')
			->addCont(HtmlLabel::get()->addCont('文件上传key')->addBr()->addCont(HtmlLabel::get()->setFontsize(12)->addCss('hui')->addCont('rockfile_key'))->addStyle('margin-right','10px')->setWidth(120)->setAlign('right'))
			->addCont(HtmlInput::gets()->setTishi('用不到不用设置')->setValue(Base::getConfig('rockfile_key'))->addStyle('flex', 1))
		);
		
		$sdiv = HtmlLabel::get()->setWidth(450);
		$sdiv->addCont(HtmlLabel::get()->addCont($mdiv2)->addCss('panel')->setPadding('20px 30px'));
		
		$mdiv->addCont($sdiv);
		
		$mdiv->addBlank(20);
		$mdiv->addCont(
			HtmlLabel::get()->addCont('系统的配置文件：'.ROOT_PATH.'/config/config.php')
		);
		return $mdiv->show();
	}
	
	/**
	*	系统日志显示
	*/
	public function syslogAction()
	{
		$file = Jiami::base64decode(Request::get('file'));
		if(!file_exists($file))$file = ROOT_PATM.'/'.$file;
		if(!file_exists($file))return MsgView::error('日志文件不存在了','关闭','javascript:window.close()');
		
		$html = new HtmlBase();
		$html->setTitle('日志文件查看');
		$html->addCont(
			HtmlLabel::get()->setDisplay('flex')->setMargin(10)->addCont(
				HtmlTextarea::gets()->addStyle('flex',1)->addStyle('line-height','24px')->setHeight('600')
				->setBackgroundColor('#caeccb')
				->addCont(htmlspecialchars(file_get_contents($file)))
			)
		);
		return $html;
	}
	
	/**
	*	系统文件浏览
	*/
	public function filelistAction()
	{
		$div 	= HtmlLabel::get();
		
		$mdiv 	= HtmlLabel::get()->addCss('panel')->addStyle('padding','10px')->setAlign('left')->addCont('主目录:')->addKong()->addCont(HtmlLabel::get('a')->setClickevt('genmulu')->addCont(ROOT_PATM)->setHref('javascript:;'))->addCont(HtmlLabel::get('span')->addCont('&gt;')->addCss('hui'));
		$mdiv ->addCont(HtmlLabel::get('span')->addCont('')->setId('path_{rand}'));
		$div->addCont($mdiv);
		
		$mdiv = HtmlLabel::get()->addCss('panel')->addStyle('margin-top','10px')->setAlign('left');
		$mdiv->addCont(HtmlLabel::get()->addStyle('padding','10px')->setId('table_view{rand}'));
		$div->addCont($mdiv);
		
		return $div->show();
	}
	
	public function filelistdataAction()
	{
		$path = Jiami::base64decode(Request::get('path'));
		$npath= ROOT_PATM;
		$rows = array();
		if($path){
			$npath .= '/'.$path.'';
			$files= File::getFolder($npath);
			$farr = array();
			$smarr[''.PACKPATH.'/config'] = '配置文件目录';
			$smarr[''.PACKPATH.'/config/version.php'] = '版本文件';
			$smarr[''.PACKPATH.'/config/config.php'] = '配置文件';
			$smarr[''.PACKPATH.'/config/config_local.php'] = '本地配置文件';
			$smarr[''.PACKPATH.'/logs'] = '日志目录';
			$smarr[''.PACKPATH.'/logs/list'] = '日志文件目录';
			$smarr[''.PACKPATH.'/logs/cache'] = '数据缓存目录';
			$smarr[''.PACKPATH.'/Third'] = '第三方程序目录';
			$smarr[''.PACKPATH.'/Module'] = '模块配置目录';
			$smarr[''.PACKPATH.'/Model'] = '数据Model目录';
			$smarr[''.PACKPATH.'/Controller'] = '控制器目录';
			$smarr[''.PACKPATH.'/Chajian'] = '插件目录';
			$smarr[''.PACKPATH.'/'.UPDIR.''] = '上传目录';
			foreach($files as $val){
				$filev 	= $npath.'/'.$val;
				$ismulu = is_dir($filev) ? 1 : 0;
				$vpath	= $val;
				if($path)$vpath	= $path.'/'.$val;
				$editdt = '';
				if($ismulu==0){
					$editdt = date('Y-m-d H:i:s', fileatime($filev));
					$editdt = CDate::stringdt($editdt);
				}
				$sarr  = array(
					'name' 	 => $val,
					'path'	 => Jiami::base64encode($vpath),
					'ismulu' => $ismulu,
					'size' 	 => ($ismulu == 0) ? Rock::formatsize(filesize($filev)) : '',
					'editdt' => $editdt,
					'sm'	 => $this->arrvalue($smarr, $vpath)
				);
				if($ismulu==1)$rows[] = $sarr;
				if($ismulu==0)$farr[] = $sarr;
			}
			$barr['countmulu'] 	= count($rows);
			$barr['countfile'] 	= count($farr);
			foreach($farr as $k=>$v)$rows[] = $v;
		}else{
			$val 	= str_replace(ROOT_PATM.'/','',ROOT_PATH);
			$rows[] = array('ismulu' => 1,'size' => '','sm'=>'根目录','editdt' => '','path' => Jiami::base64encode($val),'name' => $val,);
			
			$val 	= str_replace(ROOT_PATM.'/','',ROOT_PACK);
			$rows[] = array('ismulu' => 1,'size' => '','sm'=>'主程序目录','editdt' => '','path' => Jiami::base64encode($val),'name' => $val,);
			
			$val 	= 'vendor';
			$rows[] = array('ismulu' => 1,'size' => '','sm'=>'composer安装目录','editdt' => '','path' => Jiami::base64encode($val),'name' => $val,);
			$barr['countmulu'] 	= 3;
			$barr['countfile'] 	= 0;
		}
		$barr['totalCount'] = 0;
		$barr['rows'] 		= $rows;
		$barr['path'] 		= $path;
		return $this->returnsuccess($barr);
	}
}